package com.health.work.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * 医生回复问题界面
 * @author yinxing
 *
 */
public class AnswerActivity extends Activity{
	private Button back;
	private Button answer;
	private EditText content;
	private Thread thread;
	private String mobilePhoneNum;
	private String Question;
	private String consultationId;
	private String role;
	
	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				answer.setText("回复");
				break;
			case FAILE:
				TipsUtil.showAlert(AnswerActivity.this, null, "数据请求失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(AnswerActivity.this, null, "未知错误！");
				break;
			default:
				break;
			}

		}

	};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.answer_laylout);
		mobilePhoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		initPage();
	}

	private void initPage() {

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			role = (String) bundle.getString("role");
			consultationId = (String) bundle.getString("consultationId");
		}
		back = (Button) findViewById(R.id.btnBack);
		answer=(Button)findViewById(R.id.btnOK);
		answer.setText("回复");
		content=(EditText)findViewById(R.id.etFeedBack);
		back.setOnClickListener(onClickListener);
		answer.setOnClickListener(onClickListener);
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnBack:
				finish();
				break;
			case R.id.btnOK:
				if(answer.getText().equals("回复")){
					answer.setText("提交");
					content.setVisibility(View.VISIBLE);
				}else{
					Question=content.getText().toString();
					if(!"".equals(Question)){
						getData();
					}else{
						Toast.makeText(AnswerActivity.this, "回复内容不能为空", Toast.LENGTH_SHORT).show();
					}
					
				}
				break;
			default:
				break;
			}

		}
	};
	/**
	 * 回复提问
	 */
	private void getData() {
		thread=new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
			new RequestParamtersUtil().Reply(consultationId, mobilePhoneNum, Question, role,"1", new DefaultJSONData() {
				
				@Override
				public void parse(String result) throws JSONException {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void parse(JSONObject object) throws JSONException {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void parse(JSONArray array) throws JSONException {
					// TODO Auto-generated method stub
					
				}
			});
			}
		});
		thread.start();
	}
	
}
